package club.tclyq.good_time.photowall.type.service;

import club.tclyq.good_time.photowall.type.repository.PhotoTypeRepository;
import club.tclyq.good_time.upload.bean.PhotoType;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.springframework.beans.NullValueInNestedPathException;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Service;

import javax.validation.ValidationException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tanchong
 * Create Date: 2019/2/24
 */
@Service
public class PhotoTypeService {

    private static final String PHOTO_TYPE_COLLECTION = "photo_type";
   // private MongoOperations mongoOperations;
//    private MongoCollection<Document> photoTypeCollection;
    private PhotoTypeRepository photoTypeRepository;

    public PhotoTypeService(
         //   MongoOperations mongoOperations,
                            PhotoTypeRepository photoTypeRepository) {
        //photoTypeCollection = mongoOperations.getCollection(PHOTO_TYPE_COLLECTION);
        this.photoTypeRepository = photoTypeRepository;
    }

    public void createPhotoType(PhotoType photoType) throws IllegalAccessException {
        if(photoType.getPhoteType().isEmpty()){
            throw new IllegalArgumentException("非法参数:类型不能为空！");
        }
        if(checkType(photoType.getPhoteType())){
            throw new IllegalAccessException("非法参数:输入类型已存在！");
        }
        photoTypeRepository.insert(photoType);
    }

    public List<PhotoType> getAll(){
        return photoTypeRepository.findAll();
    }

    public boolean checkType(String type){
        var photoType = new PhotoType();
        photoType.setPhoteType(type);
       return photoTypeRepository.exists(Example.of(photoType));
    }
}
